<template>
    <div>
        <div class="buttonTop">
            <el-button type="primary" round @click="dialogVisible = true">添加用户</el-button>
            <el-button type="warning" round @click="batchDel()">批量删除</el-button>
        </div>

        <!-- 编辑的弹窗 -->
        <el-dialog
            title="编辑"
            :visible.sync="dialogVisible2"
            width="30%"
            :before-close="handleClose"
        >
            <el-form ref="form" :model="updateForm" label-width="80px" :rules="updateRules">
                <!-- id -->
                <!-- <el-row>
                    <el-col :span="2">&nbsp;</el-col>
                    <el-col :span="18">
                        <el-form-item label="id" prop="id">
                            <el-input
                                prefix-icon="el-icon-user"
                                v-model="updateForm.id"
                                placeholder
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">&nbsp;</el-col>
                </el-row>-->
                <!-- 用户名 -->
                <el-input type="hidden" v-model="updateForm.oldname" placeholder></el-input>
                <el-row>
                    <el-col :span="2">&nbsp;</el-col>
                    <el-col :span="18">
                        <el-form-item label="用户名" prop="username">
                            <el-input
                                prefix-icon="el-icon-user"
                                v-model="updateForm.username"
                                placeholder="请输入用户名"
                                :disabled="disabled"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">&nbsp;</el-col>
                </el-row>

                <!-- 密码 -->
                <!-- <el-row>
                    <el-col :span="2">&nbsp;</el-col>
                    <el-col :span="18">
                        <el-form-item label="旧密码">
                            <el-input
                                prefix-icon="el-icon-lock"
                                v-model="updateForm.password"
                                type="password"
                                placeholder="请输入密码"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">&nbsp;</el-col>
                </el-row>-->

                <!-- 确认密码 -->
                <el-row>
                    <el-col :span="2">&nbsp;</el-col>
                    <el-col :span="18">
                        <el-form-item label="新密码">
                            <el-input
                                prefix-icon="el-icon-lock"
                                v-model="updateForm.newPassword"
                                type="password"
                                placeholder="请输入密码"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">&nbsp;</el-col>
                </el-row>

                <!-- 更新按钮 -->
                <el-row>
                    <el-col :span="2">&nbsp;</el-col>
                    <el-col :span="16">
                        <el-button type="primary" @click="update()">更改</el-button>
                    </el-col>
                    <el-col :span="6">&nbsp;</el-col>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible2 = false">取 消</el-button>
            </span>
        </el-dialog>

        <el-dialog title="注册" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
            <el-form ref="form" :model="form" label-width="80px" :rules="rules">
                <!-- 用户名 -->
                <el-row>
                    <el-col :span="2">&nbsp;</el-col>
                    <el-col :span="18">
                        <el-form-item label="用户" prop="username">
                            <el-input
                                prefix-icon="el-icon-user"
                                v-model="form.username"
                                placeholder="请输入用户名"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">&nbsp;</el-col>
                </el-row>

                <!-- 密码 -->
                <el-row>
                    <el-col :span="2">&nbsp;</el-col>
                    <el-col :span="18">
                        <el-form-item label="密码">
                            <el-input
                                prefix-icon="el-icon-lock"
                                v-model="form.password"
                                type="password"
                                placeholder="请输入密码"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">&nbsp;</el-col>
                </el-row>

                <el-row>
                    <el-col :span="2">&nbsp;</el-col>
                    <el-col :span="16">
                        <el-button type="primary" @click="register()">提交</el-button>
                    </el-col>
                    <el-col :span="6">&nbsp;</el-col>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
            </span>
        </el-dialog>

        <el-table
            ref="multipleTable"
            :data="userData"
            tooltip-effect="dark"
            style="width: 100%"
            border
            @selection-change="handleSelectionChange"
        >
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column prop="id" label="编号" width="120"></el-table-column>

            <el-table-column prop="username" label="姓名" width="120"></el-table-column>
            <el-table-column prop="createTime" label="创建日期" width="200"></el-table-column>
            <el-table-column prop="updateTime" label="更新日期" width="200"></el-table-column>

            <el-table-column label="操作" show-overflow-tooltip>
                <template slot-scope="scope">
                    <el-button size="mini" @click="edit(scope.row)">编辑</el-button>
                    <el-button
                        size="mini"
                        type="danger"
                        @click="handleDelete(scope.$index, scope.row)"
                    >删除</el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>
<script>
import { addAdmin, updateAdmin, selectAll, del } from "@/api/admin/admin.js";

export default {
    created() {
        selectAll().then(response => {
            // console.log(response.data.data)
            this.userData = response.data.data;
        });
    },
    methods: {
        // 用户注册AJAX
        register() {
            let user = {
                username: "",
                password: ""
            };
            (user.username = this.form.username),
                (user.password = this.form.password),
                addAdmin(user).then(response => {
                    alert(response.data.msg);
                });
            // this.$refs["form"].resetFields();
            //重新加载列表
            selectAll().then(response => {
                this.userData = response.data.data;
            });
            //关闭对话框
            // this.dialogVisible = false;
        },
        handleDelete(index, row) {
            this.$confirm("确认删除？")
                .then(() => {
                    let list = new Array();
                    list.push(row.id);
                    del(list).then(response => {
                        alert(response.data.msg);
                    });
                })
                .catch(() => {});
            //重新加载列表
            selectAll().then(response => {
                this.userData = response.data.data;
            });
        },
        // 获取多选框中的内容
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        batchDel() {
            // this.multipleSelection.forEach(element => {
                // this.userData.forEach((item, index) => {
                //     if (element.username == item.username) {
                //         this.userData.splice(index, 1);
                //     }
                // });
            // });

            let ids = new Array();
            this.multipleSelection.forEach(element => {
                ids.push(element.id)
            })
            del(ids).then(response => {
                alert(response.data.msg);
            })
            //重新加载列表
            selectAll().then(response => {
                this.userData = response.data.data;
            });
        },
        handleClose(done) {
            this.$confirm("确认关闭？")
                .then(() => {
                    done();
                })
                .catch(() => {});
        },
        // 编辑操作
        edit(row) {
            this.dialogVisible2 = true;
            console.log(row.username + "--" + row.id);
            //将原始名字放到隐藏字段 供以后调用
            this.updateForm.oldname = row.username;
            this.updateForm.username = row.username;
            this.updateForm.id = row.id;
        },
        // 提交修改
        // TODO 不提供修改用户名功能
        update() {
            let user = {
                // oldname: '',
                // username: "",
                password: ""
            };

            user.username = this.updateForm.username;
            user.password = this.updateForm.newPassword;
            updateAdmin(user).then(response => {
                alert(response.data.msg);
            });
            //更新完之后重新加载列表
            selectAll().then(response => {
                this.userData = response.data.data;
            });
            this.dialogVisible2 = false;

            // console.log('info: ' + username +'--'+ password + '--' + newPassword);
        }
    },
    data() {
        return {
            disabled: true,
            form: {
                id: "",
                username: "",
                password: "",
                createTime: ""
            },
            updateForm: {
                id: "",
                oldname: "",
                username: "",
                password: "",
                newPassword: ""
            },
            rules: {
                username: [
                    { required: true, message: "请输入用户名", trigger: "blur" }
                ]
            },
            updateRules: {
                username: [
                    { required: true, message: "不能为空", trigger: "blur" }
                ]
                // password: [
                //     {required: true, message: '不能为空', trigger: 'blur'}
                // ],
                // newPassword: [
                //     {required: true, message: '不能为空', trigger: 'blur'}
                // ]
            },
            dialogVisible: false,
            dialogVisible2: false,

            userData: [
                // {
                //     id: "1",
                //     createTime: "2019-08-13",
                //     username: "mark1"
                // },
                // {
                //     id: "2",
                //     createTime: "2019-08-13",
                //     username: "mark2"
                // },
                // {
                //     id: "3",
                //     createTime: "2019-08-13",
                //     username: "mark3"
                // }
            ],

            multipleSelection: []
        };
    }
};
</script>
<style scoped>
.el-table .warning-row {
    background: oldlace;
}

.el-table .success-row {
    background: #f0f9eb;
}
.buttonTop {
    float: left;
    margin-left: 60px;
}
</style>